import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_uikit/common/object_util/object_util.dart';
import 'package:get/get.dart';

import '../../common/app_text_style.dart';
import '../text/text_widget.dart';

/// 动作抽屉层组件
class BottomSheetActionWidget extends StatefulWidget {
  BottomSheetActionWidget(this.actionItemList,
      {super.key, this.actionItemEvent});

  /// action列表
  final List<String?> actionItemList;

  /// actionItem点击事件
  Function(String? title, int index)? actionItemEvent;

  @override
  State<BottomSheetActionWidget> createState() =>
      BottomSheetActionWidgetState();
}

class BottomSheetActionWidgetState extends State<BottomSheetActionWidget> {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          buildActionListWidget(),
          Container(
              height: 8.w,
              decoration: const BoxDecoration(color: Color(0xFFEBEDF2))),
          buildCanceWidget(),
          /// 底部安全区域
          SizedBox(height: ScreenUtil().bottomBarHeight)
        ],
      ),
    );
  }

  /// 构建ActionList组件
  Widget buildActionListWidget() {
    return MediaQuery.removePadding(
        context: context,
        removeBottom: true,
        child: ListView.builder(
            //内容适配
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            itemCount: widget.actionItemList.length,
            itemBuilder: (BuildContext context, int index) {
              String? titleStr = widget.actionItemList[index];
              return buildActionListItemWidget(titleStr, index);
            }));
  }

  /// 构建ActionListItem组件
  Widget buildActionListItemWidget(String? title, int index) {
    return InkWell(
      onTap: () {
        Get.back();

        /// ActionItem点击事件
        if (ObjectUtil.isNotEmpty(widget.actionItemEvent)) {
          widget.actionItemEvent!(title!, index);
        }
      },
      child: Column(children: [
        index != 0
            ? Container(color: const Color(0x66EBEDF2), height: 1.w)
            : const SizedBox(),
        Container(
            alignment: Alignment.center,
            padding: EdgeInsets.symmetric(horizontal: 12.w),
            height: 48.w,
            child: TextWidget(title, TS_FF272133_18_W4))
      ]),
    );
  }

  /// 构建取消按钮
  Widget buildCanceWidget() {
    return InkWell(
      onTap: () {
        /// 取消点击事件
        Get.back();
      },
      child: Container(
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(horizontal: 12.w),
          height: 48.w,
          child: TextWidget('取消', TS_FF272133_18_W4,
              textAlign: TextAlign.center)),
    );
  }
}
